<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\RentClothOrder\Failed;
use App\Admin\Extensions\RentClothOrder\Success;
use App\Models\RentClothOrder;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RentClothOrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '晒单管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new RentClothOrder);

        $grid->model()->where([['status', 1], ['type', 2]])->orderBy('id', 'desc');
        // $grid->model()->where('type', 2)->orderBy('id', 'desc');
        $grid->column('id', __('Id'));
        $grid->rentcloth()->created_at('租衣时间');
        $grid->user()->nickname('用户昵称');

        $grid->commodity()->commodity_name(__('商品名'));
        $grid->column('content', __('晒单内容'))->limit(15);
        $grid->column('img_url', __('晒单图片'))->gallery(['width' => 50]);
        $grid->column('hot_spot', __('点赞数'))->badge('warning');
        $grid->column('browse_num', __('浏览数'))->badge('success');
        $grid->column('status', __('状态'))->display(function ($value) {
            switch ($value) {
                case '1':
                    return "<span class='label label-primary'>审核中</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>已通过</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->column('created_at', __('晒单时间'));
        $grid->disableCreateButton();

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            // $filter->equal('type','类型')->radio([
            //     '' => '全部',
            //     1 => '线上',
            //     2 => '线下',
            // ]);
            $filter->between('created_at', '申请时间')->datetime();
        });
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            // 添加操作
            $actions->append(new Success($actions->getKey()));
            $actions->append(new Failed($actions->getKey()));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(RentClothOrder::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('rent_id', __('Rent id'));
        $show->field('user_id', __('User id'));
        $show->field('nickname', __('Nickname'));
        $show->field('content', __('Content'));
        $show->field('img_url', __('Img url'));
        $show->field('hot_spot', __('Hot spot'));
        $show->field('browse_num', __('Browse num'));
        $show->field('status', __('Status'));
        $show->field('comment', __('Comment'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new RentClothOrder);


        $form->textarea('content', __('评价内容'));
        $form->multipleImage('img_url', '图片')->move('images/rentclothorder/' . date("Ym/d", time()))->uniqueName()->sortable()->removable()->help('请框选图片或按住ctrl键选择多张图片,第一张图为默认封面图');
        $form->number('hot_spot', __('点赞数'));
        $form->number('browse_num', __('浏览数'));
        return $form;
    }

    //审核通过
    public function success(Request $request)
    {
        $id = $request->id;
        $brand = $request->brand;
        $rent = RentClothOrder::findOrFail($id);

        DB::beginTransaction();
        try {
            $rent->status = 2;
            $rent->save();

            DB::commit();

            return ajax_success('操作成功');

        } catch (\Exception $exception) {

            DB::rollBack();

            return ajax_error('操作失败');

        }

    }
    //审核不通过
    public function failed(Request $request)
    {
        $id = $request->id;
        $recharge = RentClothOrder::findOrFail($id);
        $recharge->status = 3;
        $recharge->comment = $request->comment;
        $res = $recharge->save();
        if ($res) {
            return ajax_success('操作成功');
        } else {
            return ajax_error('操作失败');
        }

    }
}
